

#include "string.h"


#include "uart_comm_frame.h"
#include "crc.h"
#include "ringbuff.h"

/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void comm_frame_upload(STR_COMM_FRAME *frame, STR_RINGBUF* bufhdl)
{
	uint16_t frame_id    = 0;
	uint16_t crc_value   = 0;
	
	uint16_t frame_length = ((frame->length_H << 8) + frame->length_L);
	uint16_t data_length = frame_length - 2;
	uint16_t send_length = frame_length + 0;
	
	uint8_t send_length_H = (send_length >> 8) & 0x00FF;
	uint8_t send_length_L = (send_length >> 0) & 0x00FF;
		
	static uint8_t databuf_temp[50];
	uint8_t *databuf_p = frame->databuf;
	
	static uint16_t upload_cnt = 0;
	
	upload_cnt++;
	
	frame_id = upload_cnt;

	frame->frame_ID_H = ((frame_id >> 8) & 0x00FF);
	frame->frame_ID_L = (frame_id & 0x00FF);
	
	databuf_temp[0] = frame->cmd_ID_L;
	databuf_temp[1] = frame->cmd_ID_H;
	
	for(uint8_t i=2;i<(data_length+2);i++)
	{
		databuf_temp[i] = *(databuf_p++);
	}
	
	crc_value = GetCRC((uint8_t*)(databuf_temp), frame_length);
	
	frame->CRC_H = ((crc_value >> 8) & 0x00FF);
	frame->CRC_L = (crc_value & 0x00FF);
	
	ringBufWrite(bufhdl, (char*)frame, 4);
	
	ringBufWrite(bufhdl, (char*)&send_length_L, 1);
	ringBufWrite(bufhdl, (char*)&send_length_H, 1);
	
    ringBufWrite(bufhdl, (char*)(&frame->cmd_ID_L), 1);
	ringBufWrite(bufhdl, (char*)(&frame->cmd_ID_H), 1);
	
	ringBufWrite(bufhdl, (char*)frame->databuf, data_length);
	
    ringBufWrite(bufhdl, (char*)(&frame->CRC_L), 1);	
	ringBufWrite(bufhdl, (char*)(&frame->CRC_H), 1);
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
uint8_t comm_frame_store(STR_COMM_FRAME *frame, uint8_t *databuf)
{
	uint8_t store_length = 0;
	uint8_t crc_rslt;
	uint16_t data_length = ((frame->length_H << 8) + frame->length_L) - 2;
	
	#if 0
	frame->length_L = databuf[4];
	frame->length_H = databuf[5];
	#endif

	frame->CRC_L = databuf[8 + data_length + 0];
	frame->CRC_H = databuf[8 + data_length + 1];	
	
	crc_rslt = checkCRC(&databuf[6], (data_length + 2), ((frame->CRC_H << 8) | (frame->CRC_L)));
	
	if(crc_rslt != 0)
	{
		frame->head_H     = databuf[0];
		frame->head_L     = databuf[1];
		frame->frame_ID_L = databuf[2];
		frame->frame_ID_H = databuf[3];
		
		frame->cmd_ID_L   = databuf[6];
		frame->cmd_ID_H   = databuf[7];
		
		memcpy(frame->databuf, &databuf[8], data_length);
		
	    store_length = ((frame->length_H << 8) + frame->length_L);
	}
	
	else
	{
		store_length = 0;
	}
	
	return store_length;
}




/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

int32_t signed24_to_signed32(int32_t input)
{

	if((input & 0x800000) == 0x800000) 
	{
		input |= 0xff000000; 
	}
	
	return input;
}
			
/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/


int16_t i16_LSB_to_MSB(int16_t data)
{
	int16_t data_convert = 0;
	
	#if 0
	data_convert = 	((data & 0x00FF) << 8)  + 
					((data & 0xFF00) >> 8);
	#else
	data_convert = data;
	#endif
	
	return data_convert;
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/


int32_t i32_LSB_to_MSB(int32_t data)
{
	int32_t data_convert = 0;
	
	#if 0
	data_convert =  ((data & 0x000000FF) << 24) + 
					((data & 0x0000FF00) << 8)  + 
					((data & 0x00FF0000) >> 8)  + 
					((data & 0xFF000000) >> 24);
	#else
	data_convert = data;
	#endif

	return data_convert;
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
int64_t i64_LSB_to_MSB(int64_t data)
{
	int64_t data_convert = 0;
	
	#if 0
	data_convert =  ((data & 0x00000000000000FF) << 56)  + 
					((data & 0x000000000000FF00) << 40)  + 
					((data & 0x0000000000FF0000) << 24)  + 
					((data & 0x00000000FF000000) << 8)   + 
					((data & 0x000000FF00000000) >> 8)   + 	
					((data & 0x0000FF0000000000) >> 24)  + 
					((data & 0x00FF000000000000) >> 40)  + 	
					((data & 0xFF00000000000000) >> 56);
	#else
	data_convert = data;
	#endif

	return data_convert;
}

